<template>
<view class="page">
	<MallHeader title="我的订单"></MallHeader>
	<view class="nav-list">
		<view class="nav-item" :class="{on:navIndex === i}" v-for="(a,i) in navList" @click="navBtn(i)">
		{{a.name}}
		</view>
	</view>
	
	<block v-if="listData.length > 0">
		<view class="order-list" v-for="(item,i) in listData">
			<view class="order-item">
				<view class="shop-title">
					<view class="left">
						<image src="/static/images/mallShop/shop.png" mode="" class="icon-shop"></image>
						成都科技直营店
						<image src="/static/images/mallShop/arr_right.png" class="icon-arr" mode=""></image>
					</view>
					<view class="right">{{ item.state_text }}</view>
				</view>
				<view class="goods-list-box">
					<view class="goods-item" v-for="(a,i) in item.goods">
						<view class="img-box">
							<image :src="a.image.file_path" class="goods-img" mode=""></image>
						</view>
						<view class="goods-info">
							<view class="goods-title">{{a.goods_name}}</view>
							<view class="goods-rank">
								<view class="label">{{a.goods_attr}}</view>
								<view class="goods-num">x {{a.total_num}}</view>
							</view>
							<view class="goods-price"> CD ￥ {{a.total_price.toString().split('.')[0]}}.<text>{{a.total_price.toString().split('.')[1]}}</text></view>
						</view>
					</view>
				</view>
				<view class="goods-pay-free">
					需付款：CD ￥ {{item.total_price.split(".")[0]}}.<text class="float">{{item.total_price.split(".")[1]}}</text> + {{item.total_sharing_price.split(".")[0]}}. <text class="float">{{item.total_sharing_price.split(".")[1]}}</text>
				</view>
				<!-- <template v-if="navIndex==0||navIndex==1">
					<view class="goods-action-box">
						<view v-if="item.pay_status.value !== 20" class="btn order-btn" @click="pay(item)">付款</view>
						<view class="btn" @tap="service">联系客服</view>
						<view class="btn" v-if="item.state_text !== '已取消'" @click="cancelOrder(item.order_id, i)">取消订单</view>
					</view>
				</template> -->
				<template v-if="item.order_status.value !== 30">
					<view class="goods-action-box">
						<view v-if="item.pay_status.value === 10" class="btn order-btn" @click="pay(item)">付款</view>
						<view class="btn" v-if="item.delivery_status.value !== 20" @click="cancelOrder(item.order_id, i)">取消订单</view>
						<view class="btn order-btn" v-if="item.delivery_status.value==20 && item.receipt_status.value == 10" @tap="receipt(item, i)">确定收货</view>
						<button type="contact" @tap="service" class="btn contact-btn">联系客服</button>
						<view class="btn" v-if="item.pay_status.value !== 10" @tap="logistics()">查看物流</view>
					</view>
				</template>
			</view>
		</view>
	</block>
	
	<view class="empty" v-if="empty">
		<img src="/mall/static/images/no-goods.png" alt="">
		暂无相关商品
	</view>
</view>
</template>

<script>
import MallHeader from '@/components/mallHeader/mallHeader'
import { orderList, cancelOrder, shopDetail, receipt, onePay } from "@/api/substitution";

export default {
	components: {
		MallHeader
		},
		data() {
			return {
				navList: [{name: '全部'},{name: '待付款'},{name: '待发货'},{name: '待收货'},{name: '待评价'}],
				navIndex: 0,
				ruleForm: {
					page: 1,
					dataType: ''
				},
				listData: [],
				total: 0,
				empty: false
			}
		},
		onLoad(e) {
			this.navIndex = parseInt(e.id)
			this.ruleForm.dataType = this.getDataType(parseInt(e.id));
			uni.showLoading({
				mask: true
			})
			this.getData();
		},
		
		// 下拉刷新
		onPullDownRefresh() {
			this.ruleForm.page = 1;
			this.getData()
		},
		
		// 上拉加载
		onReachBottom() {
			console.log(this.total)
			if (this.ruleForm.page < this.total) {
				this.ruleForm.page += 1;
				this.getData()
			}
		},
		
		methods: {
			
			// 联系客服
			service () {
				uni.showToast({
					title: '该功能正在开发中',
					icon: 'none'
				})
			},
			
			// 查看物流
			logistics () {
				uni.showToast({
					title: '该功能正在开发中',
					icon: 'none'
				})
			},
			
			// 确认收货
			receipt (a, i) {
				const that = this;
				
				uni.showModal({
					title: '提示',
					content: '是否确认收货',
					success() {
						uni.showLoading({
							mask: true
						})
						receipt({
							order_id: a.order_id
						}).then(res => {
							that.listData.splice(i, 1)
						})
					}
				})
			},
			
			// 切换状态
			navBtn(i) {
				this.navIndex = i;
				this.ruleForm.dataType = this.getDataType(i);
				this.empty = false;
				this.ruleForm.page = 1;
				this.listData = [];
				uni.showLoading({
					mask: true
				})
				this.getData()
			},
			
			// 判断当前状态
			getDataType(e) {
				switch (e) {
					case 1:
						return 'payment'
						break;
					case 2:
						return 'delivery'
						break;
     case 3: 
						return 'received'
						break;
					case 4:
						return 'comment'
						break;
					default:
						return 'all'
				}
			},
			
			// 前往支付
			pay(data) {
				onePay({
					order_no: data.order_no
				}).then(res => {
					if (res.code === 1) {
						this.getPay(res.data.payment)
					}
				})
			},
			
			// 唤醒支付
			getPay (e) {
				uni.requestPayment({
					provider: "wxpay",
					orderInfo: e,
					complete: r => {
						this.getData()
					},
				})
			},
				
			// 列表
			getData() {
				orderList(this.ruleForm).then(res => {
					if (this.ruleForm.page === 1) {
						this.listData = res.data.list.data
						this.total = res.data.list.last_page
						
						if (this.listData.length === 0) this.empty = true
					} else {
						res.data.list.data.forEach(item => {
							this.listData.push(item)
						})
					}
					uni.stopPullDownRefresh()
				})
			},
				
			// 取消订单
			cancelOrder(id, i) {
				const that = this;
				uni.showModal({
					title: "操作",
					content: "是否取消该订单",
					success(e) {
						if (e.confirm) {
							uni.showLoading({
								mask: true
							})
							cancelOrder({
								order_id: id,
							}).then(res => {
								if (res.code === 1) {
									that.listData.splice(i, 1)
									uni.showToast({
										title: res.data,
										icon: 'none',
										duration: 1000,
									})
								}
							})
						}
					}
				})
			}
  },
}
</script>

<style lang="scss" scoped>
@import '/common/css/shop.css';

@mixin flexCenter() {
	display: flex;
	align-items: center;
	justify-content: center;
}

.page {
	padding-bottom: 10rpx;
	box-sizing: border-box;
}

.nav-list {
	padding: 80rpx 30rpx 30rpx;
	display: flex;
	justify-content: space-between;
	color: #999999;
	font-size: 30rpx;
	.on {
		color: #333;
		}
}

.order-list {
	padding: 0 30rpx;
	margin-bottom: 20rpx;
	.order-item {
		background-color: #FFFFFF;
		border-radius: 20rpx;
		padding-bottom: 20rpx;
		.shop-title {
			padding: 30rpx 25rpx;
			display: flex;
			justify-content: space-between;
			font-size: 32rpx;
			border-bottom: 1rpx solid #EEEEEE;
			.left {
				display: flex;
				align-items: center;
				font-weight: bold;
				.icon-shop {
					width: 36rpx;
					height: 36rpx;
					margin-right: 10rpx;
				}
				.icon-arr {
					width: 30rpx;
					height: 30rpx;
					margin-left: 10rpx;
				}
			}
			.right {
				color: #FE5638;
			}
		}
	}
	
	.goods-list-box {
		padding: 0 25rpx;
	}
	
	.goods-item {
		display: flex;
		margin-top: 30rpx;
		.goods-info {
			flex: 1;
		}
		.img-box {
			width: 180rpx;
			height: 180rpx;
			border-radius: 20rpx;
			overflow: hidden;
			margin-right: 20rpx;
			.goods-img {
				width: 180rpx;
				height: 180rpx;
			}
		}
		.goods-title {
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			text-overflow: ellipsis;
			line-height: 1.4;
			overflow: hidden;
			font-size: 32rpx;
			color: #333333;
		}
		.goods-rank {
			display: flex;
			justify-content: space-between;
			margin-top: 4rpx;
			.label {
				color: #999999;
				font-size: 26rpx;
			}
			.goods-num {
				color: #999999;
				font-size: 26rpx;
				white-space: nowrap;
			}
		}
		.goods-price {
			color: #545454;
			font-size: 32rpx;
			
			uni-text {
				font-size: 28rpx;
			}
		}
	}

	.goods-pay-free {
		color: #F13A1D;
		font-size: 28rpx;
		text-align: right;
		padding-right: 25rpx;
		margin-top: 40rpx;
		.float {
			font-size: 26rpx;
			}
		}
		.goods-action-box {
			margin-top: 30rpx;
			display: flex;
			flex-direction: row-reverse;
			padding-right: 30rpx;
			justify-content: end;
			.btn {
				width: 180rpx;
				height: 60rpx;
				margin-right: 0;
				font-size: 32rpx;
				@include flexCenter();
				border-radius: 30rpx;
				border: 1rpx solid #CCCCCC;
				color: #999999;
				+.btn {
					margin-right: 15rpx;
				}
				&.order-btn {
					color: #FFFFFF;
					border: 1rpx solid #FE4A32;
					background: linear-gradient(-90deg, #FE4A32 0%, #FE7E48 100%);
				}
				&.contact-btn {
					margin-left: 0;
					line-height: 60rpx;
					height: 64rpx;
					background-color: #FFFFFF;
					font-size: 28rpx;
				}
			}
		}
	}

	.empty {
		min-height: 600rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		font-size: 30rpx;
		color: gray;
			
		img {
			width: 300rpx;
			margin-bottom: 30rpx;
		}
	}
</style>
